﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

//using System.Data;
namespace PrinteMaster
{
    public partial class 主窗口 : Form
    {
        public static TextBox LogText;
        Network n;
        System.Net.NetworkInformation.Ping m_Ping = new System.Net.NetworkInformation.Ping();
        public 主窗口()
        {
            InitializeComponent();
            LogText = textBoxLog;
            //DataTIme dt;
            //Log.outLog("输出一下");
            n = new Network();
            n.create();
            n.send("caonima");
            客服窗口.create(m_MangerInfo.name, this);
        }




        //private string m_lpstrIDHint,m_lpstrPWHint;//,m_lpstrID,m_lpstrPW,m_lpstrUserName;
        private MySqlHelper.ManagerInfo m_MangerInfo;
        private void Form1_Load(object sender, EventArgs e)
        {

            //DataTime.Now.ToString();
        }

        
        /// <summary>
        /// 登陆模块
        /// </summary>
        /// <param name="lpstrID">账号</param>
        /// <param name="lpstrPW">密码</param>
        private void login(string lpstrID, string lpstrPW)
        {
            int req = LOGIN_ERROR_UNKNOWN;
            DataSet ds;
            try
            {
                ds = MySqlHelper.GetDataSet(MySqlHelper.Conn, CommandType.Text, "select * from 员工账号表", null);//.Tables[0].DefaultView;
            }
            catch (Exception e)
            {
            #if DEBUG
                Console.Write(e.Message);
            #endif
                MessageBox.Show("系统发生未知错误，错误信息:" + e.Message);
                return;
            }
            MySqlHelper.ManagerInfo[] m = MySqlHelper.GetInfo(ds.Tables[0]);
            MySqlHelper.ManagerInfo info = new MySqlHelper.ManagerInfo();
            for (int i = 0; i < m.Length; i++)
            {
                // 匹配账号
                if (lpstrID.Equals(m[i].user))
                {
                    //匹配密码
                    if(lpstrPW.Equals(m[i].password))
                    {
                        //全部匹配成功
                        //m_lpstrUserName
                        m_MangerInfo = m[i];
                        req = LOGIN_SUSSESS;
                        info = m[i];
                        break;
                    }
                }

                if (i == m.Length - 1)
                {
                    // 到最后一个还没有匹配出合适的账户
                    req = LOGIN_ERROR_ID;
                }

            }


            switch(req)
            {
                case LOGIN_SUSSESS:
                    // 登陆成功
                    //MessageBox.Show(STR_LOGIN_SUSSESS);
                    客服窗口.create(m_MangerInfo.name,this);
                    Log.managerLog("登陆客户端", info.id, info.name, info.user);
                    this.Hide();
                    break;
                case LOGIN_ERROR_PASSWORD:
                    MessageBox.Show(STR_LOGIN_ERROR_PASSWORD);
                    break;
                case LOGIN_ERROR_ID:
                    MessageBox.Show(STR_LOGIN_ERROR_ID);
                    break;
                case LOGIN_ERROR_NETWORK:
                    MessageBox.Show(STR_LOGIN_ERROR_NETWORK);
                    break;
                case LOGIN_ERROR_UNKNOWN:
                    MessageBox.Show(STR_LOGIN_ERROR_UNKNOWN);
                    break;
            }

        }
        /// <summary>
        /// 登陆成功
        /// </summary>
        public const int LOGIN_SUSSESS = 0;
        /// <summary>
        /// 密码错误
        /// </summary>
        public const int LOGIN_ERROR_PASSWORD = 1;
        /// <summary>
        /// 账号错误
        /// </summary>
        public const int LOGIN_ERROR_ID = 2;
        /// <summary>
        /// 网络错误
        /// </summary>
        public const int LOGIN_ERROR_NETWORK = 3;
        /// <summary>
        /// 未知错误
        /// </summary>
        public const int LOGIN_ERROR_UNKNOWN = 4;

        public const string STR_LOGIN_SUSSESS = "登陆成功";
        public const string STR_LOGIN_ERROR_PASSWORD = "密码错误";
        public const string STR_LOGIN_ERROR_ID = "您的账号或密码错误，请联系管理员";
        public const string STR_LOGIN_ERROR_NETWORK = "网络错误";
        public const string STR_LOGIN_ERROR_UNKNOWN = "未知错误";
        
        
        
        #region 事件映射

        private void btn登陆_Click(object sender, EventArgs e)
        {
            //login(textBox账号.Text, textBox密码.Text);
        }

        private void textBox密码_TextChanged(object sender, EventArgs e)
        {

        }
        
        private void textBox账号_Click(object sender, EventArgs e)
        {


        }

        private void textBox账号_Enter(object sender, EventArgs e)
        {
            //Function.inputText(textBox账号, m_lpstrIDHint);
        }

        private void textBox账号_Leave(object sender, EventArgs e)
        {
            textBox账号_Enter(null, null);
        }

        private void textBox密码_Leave(object sender, EventArgs e)
        {
            textBox密码_Enter(null, null);
        }

        private void textBox密码_Click(object sender, EventArgs e)
        {

        }

        private void textBox密码_Enter(object sender, EventArgs e)
        {
            //Function.inputPassWord(textBox密码, m_lpstrPWHint);
        }

        private void label1_Click(object sender, EventArgs e)
        {
            修改密码窗口.create(this);
        }

        private void textBox账号_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox密码_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyCode == Keys.Enter)
                btn登陆_Click(null, null);
        }

        private void textBox账号_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                btn登陆_Click(null, null);
        }

        private void textBox密码_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                btn登陆_Click(null, null);
        }

        private void 主窗口_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                btn登陆_Click(null, null);
        }

        private void btn登陆_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                btn登陆_Click(null, null);
        }

        #endregion

        private void 主窗口_FormClosed(object sender, FormClosedEventArgs e)
        {
            n.destroy();
        }

        private void timer1sec_Tick(object sender, EventArgs e)
        {
            try
            {
                MySqlHelper.ExecuteNonQuery("select * from mytesttable");
                setDatabaseState(true);
            }
            catch(Exception ex)
            {
                setDatabaseState(false);
                Log.outLog(ex.Message);
            }
            setServerState();
        }

        private void setDatabaseState(bool state)
        {
            if (state)
            {
                labelPing地址.ForeColor = Color.Green;
                labelPing地址.Text = "良好";
            }
            else
            {
                labelPing地址.ForeColor = Color.Red;
                labelPing地址.Text = "错误";
            }
        }
        public void setServerState()
        {
            if (Network.bLisRun)
            {
                label监听状态.ForeColor = Color.Green;
                label监听状态.Text = "监听中";
            }
            else
            {
                label监听状态.ForeColor = Color.Red;
                label监听状态.Text = "断开";
            }
        }
    }
}
